package com.ujs.shc.service.impl;

import com.ujs.shc.dao.UserMapper;
import com.ujs.shc.pojo.User;
import com.ujs.shc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public int registUser(User user) {

        //如果没有提交数据则注册失败
        if("".equals(user.getPhone())){
            return 0;
        }
        //1.查看是否已经注册过了
        User registedUser = userMapper.getUserBypPhone(user.getPhone());
        if(registedUser != null){
            return 2;
        }else{

            int insertedValue = userMapper.insertUser(user);

            if(insertedValue > 0){
                return 1;


            }else{
                return 0;
            }
        }
        //注册过了返回code = 2
        //没有注册则补充字段 完成注册 成功返回1，失败返回0
    }

    @Override
    public User login(String phone, String password) {

        return userMapper.getUserBypPhoneAndPassword(phone, password);

    }

    @Override
    public User getUserByPhone(String phone) {

        return userMapper.getUserBypPhone(phone);
    }

    @Override
    public Integer chageUserInfo(Integer uid, String username, String qq) {
        return userMapper.updateUserByUid(uid, username, qq);
    }

    @Override
    public User getUserByUid(Integer uid) {

        return userMapper.selectUserByUid(uid);
    }
}
